package com.example.playbox

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.playbox.bean.Student
import com.example.playbox.utils.StudentDbHelper

class UpdateIFActivity : AppCompatActivity() {

    private lateinit var etStuName: EditText
    private lateinit var etMajor: EditText
    private lateinit var etPhone: EditText
    private lateinit var etQq: EditText
    private lateinit var etAddress: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_update_ifactivity)

        val btnBack: Button = findViewById(R.id.btn_back)
        btnBack.setOnClickListener { finish() }

        val tvStuNumber: TextView = findViewById(R.id.tv_stu_number)
        tvStuNumber.text = intent.getStringExtra("stu_number2")

        etStuName = findViewById(R.id.et_stu_name)
        etMajor = findViewById(R.id.et_stu_major)
        etPhone = findViewById(R.id.et_stu_phone)
        etQq = findViewById(R.id.et_stu_qq)
        etAddress = findViewById(R.id.et_stu_address)

        val dbHelper = StudentDbHelper(applicationContext, StudentDbHelper.DB_NAME, null, 1)
        val students = dbHelper.readStudents(tvStuNumber.text.toString())

        if (students != null) {
            for (student in students) {
                etStuName.setText(student.stuName)
                etMajor.setText(student.stuMajor)
                etPhone.setText(student.stuPhone)
                etQq.setText(student.stuQq)
                etAddress.setText(student.stuAddress)
            }
        }

        val btnSaveInfo: Button = findViewById(R.id.btn_save_info)
        btnSaveInfo.setOnClickListener {
            if (checkInput()) {
                val student = Student(
                    stuNumber = tvStuNumber.text.toString(),
                    stuName = etStuName.text.toString(),
                    stuMajor = etMajor.text.toString(),
                    stuPhone = etPhone.text.toString(),
                    stuQq = etQq.text.toString(),
                    stuAddress = etAddress.text.toString()
                )
                dbHelper.saveStudent(student)
                Toast.makeText(applicationContext, "用户信息保存成功!", Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }

    private fun checkInput(): Boolean {
        val stuName = etStuName.text.toString().trim()
        val stuMajor = etMajor.text.toString().trim()
        val stuPhone = etPhone.text.toString().trim()
        val stuQq = etQq.text.toString().trim()
        val stuAddress = etAddress.text.toString().trim()

        return when {
            stuName.isEmpty() -> {
                Toast.makeText(this, "姓名不能为空!", Toast.LENGTH_SHORT).show()
                false
            }
            stuMajor.isEmpty() -> {
                Toast.makeText(this, "专业不能为空!", Toast.LENGTH_SHORT).show()
                false
            }
            stuPhone.isEmpty() -> {
                Toast.makeText(this, "联系方式不能为空!", Toast.LENGTH_SHORT).show()
                false
            }
            stuQq.isEmpty() -> {
                Toast.makeText(this, "QQ号不能为空!", Toast.LENGTH_SHORT).show()
                false
            }
            stuAddress.isEmpty() -> {
                Toast.makeText(this, "地址不能为空!", Toast.LENGTH_SHORT).show()
                false
            }
            else -> true
        }
    }
}